package com.cn.wanxi.util;

import org.omg.CORBA.PUBLIC_MEMBER;

import java.sql.*;

public class JDBC {
    /**
     * 手写jdbc的步骤
     * 1.连接数据
     * 2.加载驱动MySQL
     * 3.配置连接数据库的参数
     * 4.查询数据
     */
    private static String url = "jdbc:mysql://121.199.55.96:3306/canyin04?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=UTC";
    private static String user = "canyin04";
    private static String password = "123456";
    private static String driver = "com.mysql.cj.jdbc.Driver";

    private static Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static ResultSet find(String sql) {
        Connection connection = getConnection();
        ResultSet resultSet = null;
        try {

            //编译我们的sql语句Statement和PreparedStatement的区别
//            String aa="select * from user where id=? and username=?";

            PreparedStatement preparedStatement = connection.prepareStatement(sql);
//            preparedStatement.setInt(1,1);
//            preparedStatement.setString(4,"supeng");

            resultSet = preparedStatement.executeQuery();//执行sql语句，返回结果集
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }

    public static int update(String sql) {
        Connection connection = getConnection();
        int count = 0;
        try {
            //编译我们的sql语句Statement和PreparedStatement的区别
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            count = preparedStatement.executeUpdate();//执行sql语句，影响的条数
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    public void Close() {

    }
}
